package com.zk.algorithm.test;

import com.zk.algorithm.Utils;

/**
 * @author zk
 */
public class SMax {

    public static void main(String...args) {
        new SMax().uniquePathsWithObstacles(new int[][]{
                {0,0,0},
                {0,1,0},
                {0,0,0}
        });
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int[][] ways = new int[obstacleGrid.length][obstacleGrid[0].length];
        ways[0][0] = 1;

        for (int i = 0; i < obstacleGrid.length; i++) {
            for (int j = 0; j < obstacleGrid[i].length; j++) {
                if (i == 0 && j == 0) {
                    continue;
                }

                if (i - 1 < 0) {
                    ways[i][j] = obstacleGrid[i][j - 1] != 1 ? ways[i][j - 1] : 0;
                } else if (j - 1 < 0) {
                    ways[i][j] = obstacleGrid[i - 1][j] != 1 ? ways[i - 1][j] : 0;
                } else {
                    ways[i][j] = (obstacleGrid[i][j - 1] != 1 ? ways[i][j - 1] : 0) +
                            (obstacleGrid[i - 1][j] != 1 ? ways[i - 1][j] : 0);
                }
            }
        }

        Utils.println(ways);

        return ways[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
    }

}
